<?php

namespace app\common\library\job;

use app\common\library\TeamCount;
use think\Log;
use think\queue\Job;

class TeamCountExpertLevelJob
{

    /**
     * fire方法是消息队列默认调用的方法
     * @param Job            $job      当前的任务对象
     * @param array|mixed    $data     发布任务时自定义的数据
     */
    public function fire(Job $job, $data)
    {
        Log::write(var_export($data, true), "TeamCountExpertLevelJob", true);

        $isJobDone = $this->doJob($data);
        if ($isJobDone) {
            // 如果任务执行成功， 记得删除任务
            $job->delete();
        } else {
            if ($job->attempts() > 3) {

                $job->delete();

                // 也可以重新发布这个任务
                //print("<info>Hello Job will be availabe again after 2s."."</info>\n");
                //$job->release(2); //$delay为延迟时间，表示该任务延迟2秒后再执行
            }
        }
    }

    /**
     * 根据消息中的数据进行实际的业务处理...
     */
    private function doJob($data)
    {
        $user_array = [];
        $user_id = $data["user_id"] ?? 0;
        $expert_level = $data["expert_level"] ?? 0;
        $marketing_level = $data["marketing_level"] ?? 0;
        $is_area_manager = $data["is_area_manager"] ?? 0;

        if ($user_id) {
            $user_ids = [$user_id];
            $user_array = TeamCount::getTreeExpertLevelRelations($user_ids, $user_array);

            $expert1_count = 0;
            $expert2_count = 0;
            $expert3_count = 0;
            $expert_count = 0;

            $marketing_director_count = 0;
            $marketing_manager_count = 0;
            $corporate_partner_count = 0;
            $area_manager_count = 0;
            $team_count = count($user_array);

            if ($user_array) {
                foreach ($user_array as $user) {

                    if ($user["expert_level"] > 0) {
                        $expert_count++;
                    }
                    if ($user["expert_level"] == 1) {
                        $expert1_count++;
                    }
                    if ($user["expert_level"] == 2) {
                        $expert2_count++;
                    }
                    if ($user["expert_level"] == 3) {
                        $expert3_count++;
                    }
                    if ($user["marketing_level"] == 1) {
                        $marketing_director_count++;
                    }
                    if ($user["marketing_level"] == 2) {
                        $marketing_manager_count++;
                    }
                    if ($user["marketing_level"] == 3) {
                        $corporate_partner_count++;
                    }
                    if ($user["is_area_manager"] == 1) {
                        $area_manager_count++;
                    }
                }
            }

            $msg = "查到用户ID:" . $user_id . ",团队人数：" . $team_count . " ,一星达人的人数:" . $expert1_count . ",二星达人的人数:" . $expert2_count . ",三星达人的人数:" . $expert3_count . ",市场总监的人数:" . $marketing_director_count . ",市场经理的人数:" . $marketing_manager_count . ",合伙人的人数:" . $corporate_partner_count . ",区经理商的人数:" . $area_manager_count;

            Log::write($msg, "TeamCountExpertLevelJobResult", true);

            //总业绩
            $total_money = $expert1_count * 585 + $expert1_count * 2730 + $expert3_count * 11700;

            $user_team_count = db("user_team_count")->where("user_id", $user_id)->find();
            if ($user_team_count) {
                db("user_team_count")->where("id", $user_team_count["id"])->update([
                    "myself_expert_level" => $expert_level,
                    "myself_marketing_level" => $marketing_level,
                    "myself_area_manager" => $is_area_manager,
                    "expert_1" => $expert1_count,
                    "expert_2" => $expert2_count,
                    "expert_3" => $expert3_count,
                    "expert_count" => $expert_count,
                    "total_money" => $total_money,
                    "marketing_director_count" => $marketing_director_count,
                    "marketing_manager_count" => $marketing_manager_count,
                    "corporate_partner_count" => $corporate_partner_count,
                    "area_manager_count" => $area_manager_count,
                    "team_count" => $team_count,
                    "update_time" => time(),
                ]);
            } else {
                db("user_team_count")->insert([
                    "user_id" => $user_id,
                    "myself_expert_level" => $expert_level,
                    "myself_marketing_level" => $marketing_level,
                    "myself_area_manager" => $is_area_manager,
                    "expert_1" => $expert1_count,
                    "expert_2" => $expert2_count,
                    "expert_3" => $expert3_count,
                    "expert_count" => $expert_count,
                    "total_money" => $total_money,
                    "marketing_director_count" => $marketing_director_count,
                    "marketing_manager_count" => $marketing_manager_count,
                    "corporate_partner_count" => $corporate_partner_count,
                    "area_manager_count" => $area_manager_count,
                    "team_count" => $team_count,
                    "add_time" => time(),
                ]);
            }
        }
        return true;
    }
}
